require "common/Define"
require "framework/BaseController"
require "FairyGUI"

FilterMain = BaseController:New({mControllerName = "FilterMain"});

function FilterMain.Main()
	-- body
	FilterMain:Awake();
end

function FilterMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/Filter";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("Filter", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function FilterMain:InitView()
	local blurFilter = FairyGUI.BlurFilter();
	blurFilter.blurSize = 2;
	self.mainComponent:GetChild("n21").filter = blurFilter;

	self._s0 = self.mainComponent:GetChild("s0").asSlider;
	self._s1 = self.mainComponent:GetChild("s1").asSlider;
	self._s2 = self.mainComponent:GetChild("s2").asSlider;
	self._s3 = self.mainComponent:GetChild("s3").asSlider;
	self._s4 = self.mainComponent:GetChild("s4").asSlider;

	self._s0.value = 100;
	self._s1.value = 100;
	self._s2.value = 100;
	self._s3.value = 200;
	self._s4.value = 20;

	self._s0.onChanged:Add(self.__sliderChanged, self);
	self._s1.onChanged:Add(self.__sliderChanged, self);
	self._s2.onChanged:Add(self.__sliderChanged, self);
	self._s3.onChanged:Add(self.__sliderChanged, self);
	self._s4.onChanged:Add(self.__sliderChanged, self);
end

function FilterMain:__sliderChanged(context)
	-- body
	local cnt = self.mainComponent.numChildren;

	for i=0, cnt-1, 1 do
		local obj = self.mainComponent:GetChildAt(i);
		print(" i", i, obj.filter, obj);
		if FairyGUI.LuaUIHelper.IsType(obj.filter, "FairyGUI.ColorFilter") then
			local filter = obj.filter;
			filter:Reset();
			filter:AdjustBrightness((self._s0.value - 100.0) / 100.0);
			filter:AdjustContrast((self._s1.value - 100.0) / 100.0);
			filter:AdjustSaturation((self._s2.value - 100.0) / 100.0);
			filter:AdjustHue((self._s3.value - 100.0) / 100.0);
		elseif FairyGUI.LuaUIHelper.IsType(obj.filter, "FairyGUI.BlurFilter") then
			local filter = obj.filter;
			filter.blurSize = self._s4.value / 100.0;
		end 
	end
end

function FilterMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

